package com.lsa.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lsa.southwind.entity.Absent;
import com.lsa.southwind.entity.Building;
import com.lsa.southwind.entity.Dormitory;
import com.lsa.southwind.entity.Student;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.service.AbsentService;
import com.lsa.southwind.service.BuildingService;
import com.lsa.southwind.service.DormitoryService;
import com.lsa.southwind.service.StudentService;
import com.lsa.southwind.util.ResultVoUtil;
import com.lsa.southwind.vo.PageVo;
import com.lsa.southwind.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/absent")
public class AbsentController {
    @Autowired
    private AbsentService absentService;
    @Autowired
    private BuildingService buildingService;
    @Autowired
    private DormitoryService dormitoryService;
    @Autowired
    private StudentService studentService;

    @GetMapping("/list/{page}/{size}")
    private ResultVo list(@PathVariable Integer page, @PathVariable Integer size) {
        PageVo list = this.absentService.list(page, size);
        return ResultVoUtil.success(list);
    }

    @GetMapping("/search")
    public ResultVo search(SearchForm searchForm) {
        return ResultVoUtil.success(this.absentService.search(searchForm));
    }

    @GetMapping("/buildingList")
    public ResultVo buildingList() {
        List<Building> list = this.buildingService.list();
        return ResultVoUtil.success(list);

    }

    @GetMapping("/findDormitoryByBuildingId/{id}")
    public ResultVo findDormitoryByBuildingId(@PathVariable Integer id) {
        QueryWrapper<Dormitory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("building_id", id);
        List<Dormitory> dormitoryList = this.dormitoryService.list(queryWrapper);
        return ResultVoUtil.success(dormitoryList);
    }

    @GetMapping("/findStudentByDormitoryId/{id}")
    public ResultVo findStudentByDormitoryId(@PathVariable Integer id) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dormitory_id", id);
        List<Student> studentList = this.studentService.list(queryWrapper);
        return ResultVoUtil.success(studentList);
    }

    @PostMapping("/save")
    public ResultVo save(@RequestBody Absent absent) {
        String createDate = absent.getCreateDate();
        createDate = createDate.substring(0, 10);
        absent.setCreateDate(createDate);
        boolean save = this.absentService.save(absent);
        if (!save) {
            return ResultVoUtil.fail();
        }
        return ResultVoUtil.success(null);
    }
}

